//
//  WBBaseViewController.swift
//  WSWeibo
//
//  Created by guojianfeng on 2017/7/13.
//  Copyright © 2017年 guojianfeng. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {
    
    var userLogon = true
    //用户没有登录就不加载tableview
    var tableView: UITableView?
    var refreshControl: UIRefreshControl?
    var isPullUp = false
    var vistorInfoDictionary: [String : String]?
    lazy var navigationBar = UINavigationBar(frame:CGRect(x: 0 ,y: 0, width:UIScreen.yw_screenWidth(), height:64))
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()        // Do any additional setup after loading the view.
    }
    
    
    override var title: String?{
        didSet{
            navItem.title = title
        }
    }
    
    func loadData() {
        endRefresh()
    }
    
    func endRefresh()  {
        refreshControl?.endRefreshing()
    }

}

extension WBBaseViewController{
    @objc func login(){
    print("login")
    }
    
    @objc func register(){
    print("register")
    }
}


extension WBBaseViewController: UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        if row == (count - 1)  && !isPullUp{
            print("上拉刷新")
            isPullUp = true
            
            loadData()
        }
        
    }
}


extension WBBaseViewController{
    fileprivate func setupUI(){
        view.backgroundColor = UIColor.yw_random()
        //隐藏导航栏缩进20个点
        automaticallyAdjustsScrollViewInsets = false
        setupNavigationBar()
        userLogon ? setupTableView() : setupVisitorView()
    }
    
    private func setupNavigationBar(){
        view.addSubview(navigationBar)
        navigationBar.items = [navItem]
        navigationBar.barTintColor = UIColor.yw_color(withHex: 0xF6F6F6)
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.darkGray]
        navigationBar.tintColor = UIColor.orange
    }
    
    func setupTableView(){
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navigationBar)
        tableView?.dataSource = self
        tableView?.delegate = self
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    fileprivate func setupVisitorView(){
        let visitorView: WBVistorView = WBVistorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navigationBar)
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.vistorInfo = vistorInfoDictionary
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .plain, target: self, action: #selector(login))
    }
    
}
